<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    function newInstance(Type, ...args) {
      // 创建一个空的Object实例对象
      const obj = {}
      // 给它添加一个__proto__属性, 值是Type的prototype
      obj.__proto__ = Type.prototype
      // 调用构造函数Type, 并指定this为新创建的对象
      const result = Type.call(obj, ...args)
      // 如果函数的返回值是对象, 返回这个对象, 否则返回新创建的对象
      return result instanceof Object ? result : obj
    }

    function Person(name, age) {
      this.name = name
      this.age = age

      // return {m: 1}
      // return []
      return () => {}
      // return 1
    }

    const p = new Person('A', 12)
    console.log(p)

    const p2 = newInstance(Person, 'B', 13)
    console.log(p2)
  </script>
</body>
</html>